<?php

class SeriesController extends Controller
{

    public $layout = '//layouts/main';
    public $defaultAction = 'manage';


    public function actionCreate()
    {
        $model = new Series();
        if (isset($_POST['Series'])) {
            $model->attributes = $_POST['Series'];
            if ($model->save()) {
                Yii::app()->user->setFlash('alert-success', 'Create Success');
                $this->redirect($this->defaultAction);
            } else {
                Yii::app()->user->setFlash('alert-failure', 'Create failed');
            }
        }
        $this->render('model', array(
            'model' => $model,
        ));
    }

    public function actionDelete()
    {
        if (Yii::app()->request->isAjaxRequest) {
            $result = 'Success';
            $id = $this->getQuery('get', 'id', null);
            $model = Series::model()->with('series_topic')->findByPk($id);
            if ($model) {
                if ($model->delete()) {
                    $result = 'Success';
                } else {
                    $result = 'delete failure';
                }
            }
            echo $result;
        } else {
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionIndex()
    {
        $this->render('index');
    }

    public function actionManage()
    {
        session_write_close();
        $model = new Series('search');
        if (isset($_GET['Series'])) {
            $model->attributes = $_GET['Series'];
        }
        if (isset($_GET['searchKey'])) {
            $model->searchKey = $_GET['searchKey'];
        }
        $this->render('manage', array(
            'model' => $model,
        ));

    }

    public function actionUpdate()
    {
        if (isset($_GET['id'])) {
            $model = Series::model()->findByPk($_GET['id']);
            if ($model) {
                if (isset($_POST['Series']) && ($model->attributes = $_POST['Series'])) {
                    if ($model->update()) {
                        Yii::app()->user->setFlash('alert-success', 'Update Success');
                        $this->redirect(array($this->defaultAction));
                    } else {
                        if ($model->hasErrors('status')) {
                            Yii::app()->user->setFlash('alert-failure', 'Update failure , the config can\'t work  and  error is ' . $model->getError('status'));
                        }
                    }
                }
                $this->render('model', array(
                    'model' => $model,
                ));
                return;
            }
        }
        Yii::app()->user->setFlash('alert-failure', "Update failure");
        $this->redirect(array($this->defaultAction));
    }

    public function actionLink()
    {
        if (isset($_GET['id'])) {
            $model = Series::model()->findByPk($_GET['id']);
            if (isset($_POST['Series']['tids']) && ($tids = $_POST['Series']['tids'])) {
                $model->tids = $tids;
                if ($model->updateTopics()) {
                    Yii::app()->user->setFlash('alert-success', 'Link Success');
                    $this->redirect(array($this->defaultAction));
                } else {
                    if ($model->hasErrors('status')) {
                        Yii::app()->user->setFlash('alert-failure', 'Link failure , the config can\'t work  and  error is ' . $model->getError('status'));
                    }
                }
            }
            $this->render('link', array(
                'model' => $model,
            ));
            return;
        }
        Yii::app()->user->setFlash('alert-failure', "Link failure");
        $this->redirect(array($this->defaultAction));
    }

    public function colRevise($data, $row, $c)
    {
        $name = $c->name;
        switch ($name) {
            case 'name':
                $html = '';
                $html .= CHtml::tag('span',['style'=>'color:'.$data->color],$data->name);
                return $html;
                break;
            case 'time_create':
                $html = '';
                $html .= date('Y:m:d H:i:s ', $data->time_create);
                return $html;
                break;
            case 'link':
                $html = '';
                $topics = $data->topics;
                $html .= sizeof($topics);
                return $html;
                break;
            case 'operations':
                $params = [];
                $res = '';
                //edit
                $params[] = Yii::app()->getController()->createUrl("update", array("id" => $data->id,));
                $res .= '<a class ="" title="编辑" href="%s"> 编辑</a>';
                //delete
                $params[] = '删除';
                $params[] = Yii::app()->getController()->createUrl("delete", array("id" => $data->id,));
                $res .= '<a class ="delete "  tooltip="delete" title="%s"  href="%s"> 删除</a>';
                $params[] = Yii::app()->getController()->createUrl("link", array("id" => $data->id,));
                $res .= '<a class="" aria-hidden="true" href="%s"> 关联主题</a>';
                return vsprintf($res, $params);
                break;
            default:
                return $data->$name;
        }
    }

// Uncomment the following methods and override them if needed
    /*
    public function filters()
    {
    // return the filter configuration for this controller, e.g.:
    return array(
    'inlineFilterName',
    array(
    'class'=>'path.to.FilterClass',
    'propertyName'=>'propertyValue',
    ),
    );
    }

    public function actions()
    {
    // return external action classes, e.g.:
    return array(
    'action1'=>'path.to.ActionClass',
    'action2'=>array(
    'class'=>'path.to.AnotherActionClass',
    'propertyName'=>'propertyValue',
    ),
    );
    }
    */
}